package com.cxyd.modules.wcs.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.constant.WcsConstants;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wcs.dto.ContDTO;
import com.cxyd.modules.wcs.dto.PinDTO;
import com.cxyd.modules.wcs.service.ContService;
import com.cxyd.modules.wcs.service.PinService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("wcs/car")
public class CarModelController {
    @Autowired
    private ContService contService;
    @Autowired
    private PinService pinService;

    @GetMapping("cont/page")
    @RequiresPermissions("wcs:cont:page")
    public Result<PageData<ContDTO>> contPage(@RequestParam Map<String, Object> params) {
        final PageData<ContDTO> page = contService.page(params);
        return new Result<PageData<ContDTO>>().ok(page);
    }

    @GetMapping("cont/{id}")
    public Result<ContDTO> getCont(@PathVariable("id") Long id) {
        ContDTO data = contService.get(id);

        return new Result<ContDTO>().ok(data);
    }

    @GetMapping("pin/{id}")
    public Result<PinDTO> getPin(@PathVariable("id") Long id) {
        PinDTO data = pinService.get(id);

        return new Result<PinDTO>().ok(data);
    }

    @GetMapping("pin/page")
    @RequiresPermissions("wcs:pin:page")
    public Result<PageData<PinDTO>> PinPage(@RequestParam Map<String, Object> params) {
        final PageData<PinDTO> page = pinService.page(params);
        return new Result<PageData<PinDTO>>().ok(page);
    }

    @PostMapping("cont/update")
    @LogOperation("更新料架车")
    @RequiresPermissions("wcs:cont:update")
    public Result contUpdate(@RequestBody ContDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        ProcResult procResult = contService.updateCont(dto);
        if (WcsConstants.PROC_CODE_SUCCESS.equals(procResult.getCode())) {
            return new Result().ok("success");
        }
        return new Result().error(500, procResult.getMsg());
    }

    @PostMapping("pin/update")
    @LogOperation("更新型号")
    @RequiresPermissions("wcs:pin:update")
    public Result pinUpdate(@RequestBody PinDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        ProcResult procResult = pinService.updatePin(dto);
        if (WcsConstants.PROC_CODE_SUCCESS.equals(procResult.getCode())) {
            return new Result().ok("success");
        }
        return new Result().error(500, procResult.getMsg());
    }
}
